package net.form.processing;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;

import javax.imageio.ImageIO;

import net.form.Pixel;

public class Image {

	private BufferedImage bufferedImage;

	/**
	 * Ancho de la imagen
	 */
	private int width;

	/**
	 * Alto de la imagen
	 */
	private int height;

	public Image(BufferedImage image) {
		super();
		this.bufferedImage = image;
		this.width = image.getWidth();
		this.height = image.getHeight();
	}

	public Image(int width, int height) {
		super();
		this.bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		this.width = width;
		this.height = height;
	}

	public BufferedImage getBufferedImage() {
		return bufferedImage;
	}

	public void setBufferedImage(BufferedImage image) {
		this.bufferedImage = image;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getRGB(int x, int y) {
		if (x >= bufferedImage.getMinX() && x < bufferedImage.getWidth()
				&& y >= bufferedImage.getMinY()
				&& y < bufferedImage.getHeight()) {
			return bufferedImage.getRGB(x, y);
		} else
			return -1;
	}

	public void setRGB(int x, int y, int rgb) {
		if (x >= 0 && y >= 0 && x < width && y < height) {
			bufferedImage.setRGB(x, y, rgb);
		}
	}

	public Raster getData() {
		return bufferedImage.getData();
	}

	public void setData(Raster data) {
		bufferedImage.setData(data);

	}

	public int getMinX() {
		return bufferedImage.getMinX();
	}

	public int getMinY() {
		return bufferedImage.getMinY();
	}

	public Image getSubimage(int x, int y, int w, int h) {
		return new Image(bufferedImage.getSubimage(x, y, w, h));
	}

	public Color getColorPunto(Pixel pixel) {
		return new Color(bufferedImage.getRGB(pixel.getX(), pixel.getY()));
	}

	/**
	 * Recupera el pixel (x,y) de la imagen
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param nbands
	 * @param pixels
	 * @return
	 */
	public Pixel getPixel(int x, int y) {
		return new Pixel(x, y, new Color(bufferedImage.getRGB(x, y)),
				getBufferedImage().getWidth(), getBufferedImage().getHeight());
	}

	/**
	 * Recupera el pixel (x,y) de la imagen
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param nbands
	 * @param pixels
	 * @return
	 */
	public Pixel getPixel(Pixel p) {
		try {
			if (p != null && p.getX() >= bufferedImage.getMinX()
					&& p.getX() < bufferedImage.getWidth()
					&& p.getY() >= bufferedImage.getMinY()
					&& p.getY() < bufferedImage.getHeight()) {
				p.setMaxX(bufferedImage.getWidth());
				p.setMaxY(bufferedImage.getHeight());
				p.setCol(getColorPunto(p));
				return p;
			}
		} catch (Exception e) {
			System.out.println("Error al obtener el pixel. "
					+ bufferedImage.getWidth() + " - "
					+ bufferedImage.getHeight() + " p es: " + p.getX() + ":"
					+ p.getY());
			e.printStackTrace();
		}
		return null;
	}
	
	public static Image loadImage(String filename) {
		Image result = null;
		try {
			result = new Image(ImageIO.read(new File(filename)));
		} catch (Exception e) {
			System.out.println(e.toString() + " Image '" + filename
					+ "' not found.");
		}
		return result;
	}
	
	public static void saveImage(String filename, Image image) {
		File file = new File(filename);
		try {
			ImageIO.write(image.getBufferedImage(), "png", file);
		} catch (Exception e) {
			System.out.println(e.toString() + " Image '" + filename
					+ "' saving failed.");
		}
	}
}
